<template>
  <div class="home" id="meun">
    <el-container>
      <el-header
        class="headBody"
        v-if="userInfo.userType == 1"
      >
        <div class="headBody_left">
          <img src="@/assets/img/dllogo.png" alt="" />
          <div>生产质量数字化监控系统</div>
        </div>
        <div class="headBody_right">
          <el-button class="head_text" type="text" icon="el-icon-s-home"
            >APP</el-button
          >
          <el-button
            class="head_text"
            type="text"
            icon="el-icon-s-home"
            @click="goHome"
            >首页</el-button
          >
          <el-button
            class="head_text"
            type="text"
            icon="el-icon-d-arrow-right"
            @click="back()"
            >退出</el-button
          >
          <el-button
            class="head_text"
            type="text"
            icon="el-icon-rank"
            @click="screen"
            >全屏</el-button
          >
        </div>
      </el-header>
      <el-main>
        <el-container style="height: 100%">
          <el-aside :width="isCollapse ? '50px' : '200px'">
            <i
              class="el-icon-s-fold toggle-button"
              @click="toggleCollapse"
              v-if="isCollapse == false"
            ></i>
            <i
              class="el-icon-s-unfold toggle-button"
              @click="toggleCollapse"
              v-else
            ></i>
            <!-- 菜单 -->
            <el-menu
              :default-active="activePath"
              router
              unique-opened
              :collapse-transition="false"
              :collapse="isCollapse"
              class="sidebar el-menu-vertical-demo"
              @select="openMenu"
              active-text-color="rgb(51, 85, 199)"
              text-color="#fff"
            >
              <!-- 一级循环 -->
              <fragment v-for="(v, i) in leftNameData" :key="i">
                <el-submenu :index="v.path ? v.path : i + ''">
                  <template slot="title">
                    <el-menu-item :index="v.path" v-if="v.path">
                      <template slot="title">
                        <div class="secondaryView">
                          <i :class="v.img" :style="{ width: v.width }"></i>
                          <span class="meneTitle" slot="title">
                            {{ v.title }}</span
                          >
                        </div>
                      </template>
                    </el-menu-item>
                    <div v-else style="display: flex">
                      <i :class="v.img" :style="{ width: v.width }"></i>
                      <span slot="title">{{ v.title }}</span>
                    </div>
                    <!-- <i :class="v.img" :style="{ 'width': v.width }"></i>
                    <span slot="title">{{ v.title }}</span> -->
                  </template>
                  <!-- 二级循环 -->
                  <div v-for="(arr, num) in v.children" :key="num">
                    <!-- 没有三级 -->
                    <div
                      v-if="arr.children.length == 0 && arr.lever == 0"
                      style="display: flex; align-items: center"
                    >
                      <!-- :style="{'padding-left':isCollapse? '':'60px'}" -->
                      <el-menu-item :index="arr.path">
                        <template slot="title">
                          <!-- 跳查看 -->
                          <div class="secondaryView">
                            <i
                              :class="arr.img"
                              :style="{ width: arr.width }"
                            ></i>
                            <span class="meneTitle" slot="title">
                              {{ arr.title }}</span
                            >
                          </div>
                        </template>
                      </el-menu-item>
                      <!-- 跳编辑 -->
                      <el-menu-item
                        v-if="arr.path2"
                        :index="arr.path2"
                        class="el-icon-edit"
                      >
                      </el-menu-item>
                    </div>
                    <div
                      v-if="arr.children.length == 0 && arr.lever == 1"
                      style="display: flex; align-items: center"
                    >
                      <el-menu-item
                        :index="arr.path"
                        style="padding-left: 23px !important"
                      >
                        <template slot="title">
                          <i :class="arr.img" :style="{ width: arr.width }"></i>
                          <div class="secondaryView">
                            <span class="meneTitle" slot="title">{{
                              arr.title
                            }}</span>
                          </div>
                        </template>
                      </el-menu-item>
                    </div>
                    <div
                      v-if="arr.children.length == 0 && arr.lever == 2"
                      class="eidtMenu"
                      style="display: flex; align-items: center"
                    >
                      <div
                        style="
                          padding-left: 23px !important;
                          width: 100%;
                          box-sizing: border-box;
                          padding-right: 15px;
                        "
                      >
                        <div
                          slot="title"
                          style="
                            display: flex;
                            width: 100%;
                            justify-content: space-between;
                          "
                        >
                          <el-menu-item :index="arr.path">
                            <template slot="title">
                              <!-- 跳查看 -->
                              <div class="secondaryView">
                                <i
                                  :class="arr.img"
                                  :style="{ width: arr.width }"
                                ></i>
                                <span class="meneTitle" slot="title">{{
                                  arr.title
                                }}</span>
                              </div>
                            </template>
                          </el-menu-item>
                          <!-- 跳编辑 -->
                          <el-menu-item
                            v-if="arr.path2"
                            :index="arr.path2"
                            class="el-icon-edit"
                          >
                            <!-- <template>
                              <div>2</div>
                            </template> -->
                          </el-menu-item>
                        </div>
                      </div>
                    </div>

                    <!-- 有三级 -->
                    <div v-else>
                      <div
                        style="
                          padding-left: 15px !important;
                          width: 100%;
                          box-sizing: border-box;
                        "
                      >
                        <div
                          slot="title"
                          style="
                            display: flex;
                            width: 100%;
                            justify-content: space-between;
                          "
                        >
                          <el-submenu
                            :index="i + num + 's1'+arr.lever"
                            v-if="arr.lever == 0"
                          >
                            <template slot="title">
                              <i
                                :class="arr.img"
                                :style="{ width: arr.width }"
                              ></i>
                              <span class="meneTitle" slot="title">{{
                                arr.title
                              }}</span>
                            </template>
                            <!-- 三级循环 -->
                            <div
                              v-for="(item, index) in arr.children"
                              :key="index"
                            >
                              <div
                                v-if="item.children.length == 0"
                                style="
                                  display: flex;
                                  padding-right: 8px !important;
                                "
                              >
                                <el-menu-item
                                  :index="item.path"
                                  style="
                                    width: 100% !important;
                                    margin-left: 18px;
                                  "
                                >
                                  <template slot="title">
                                    <!-- 跳查看 -->
                                    <div class="secondaryView">
                                      <i
                                        :class="item.img"
                                        :style="{ width: item.width }"
                                      ></i>
                                      <span class="meneTitle" slot="title">{{
                                        item.title
                                      }}</span>
                                    </div>
                                  </template>

                                  <!-- 三级循环里既有查看又有编辑 -->
                                </el-menu-item>
                                <!-- 跳编辑 -->
                                <el-menu-item
                                  v-if="item.path2"
                                  :index="item.path2"
                                  class="el-icon-edit"
                                >
                                </el-menu-item>
                              </div>
                              <!-- 还有四级循环 -->
                              <!-- 四级循环里既有查看又有编辑 -->
                              <div v-else style="width: 100%">
                                <el-submenu
                                  :index="item + num*Math.random() + 'sf1'"
                                  v-if="item.lever == 0"
                                  style="
                                    width: 94% !important;
                                    margin-left: 10px;
                                  "
                                >
                                  <template slot="title">
                                    <i
                                      :class="item.img"
                                      :style="{ width: item.width }"
                                    ></i>
                                    <span class="meneTitle" slot="title">
                                      {{
                                      item.title
                                    }}
                                    <!-- {{JSON.stringify(item)}} -->
                                    </span>
                                  </template>
                                  <!-- 四级循环里只有查看 -->
                                  <el-menu-item
                                    :index="it.path"
                                    v-for="(it, index) in item.children"
                                    :key="index +it.path + 'lever4'"
                                  >
                                    <div
                                      v-if="it.children.length == 0"
                                      style="
                                        padding-left: 18px !important;
                                        width: 100%;
                                        box-sizing: border-box;
                                        padding-right: 15px;
                                        display: flex;
                                      "
                                    >
                                      <i
                                        :class="it.img"
                                        :style="{ width: it.width }"
                                      ></i>
                                      <span class="meneTitle" slot="title">{{
                                        it.title
                                      }}</span>
                                    </div>
                                  </el-menu-item>
                                </el-submenu>
                              </div>
                            </div>
                          </el-submenu>
                        </div>
                      </div>
                    </div>
                  </div>
                </el-submenu>
              </fragment>
            </el-menu>
            <div class="userInfo">
              <div class="iconsv">
                <img
                  class="iconimg"
                  :src="userInfo.gender == 0 ? girls : boys"
                  alt=""
                />
                <!-- <img src="../assets/img/organization/boys.png" alt=""> -->
              </div>
              <div class="name">{{ userInfo.realName }}</div>
            </div>
          </el-aside>

          <el-container>
            <div class="content">
              <router-view v-if="isReloadAlive" />
            </div>
          </el-container>
        </el-container>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import {
  getBaseModuleList, //功能目录列表
} from "@/api/request.js";
export default {
  name: "Home",
  components: {
    // PositionTitle,
    // Mechanisma,
  },
  provide() {
    return {
      reload: this.reload,
    };
  },

  data() {
    return {
      isReloadAlive: true,
      // 是否全屏
      fullscreen: false,
      // 被激活的链接地址
      activePath: "/index",
      // 是否折叠
      isCollapse: false,
      fristModule: [],
      userInfo: {},
      boys: require("../assets/img/organization/boys.png"),
      girls: require("../assets/img/organization/girls.png"),
      leftNameData: this.$store.state.leftNameData,
    };
  },
  created() {
    this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
  },
  // computed: {
  //   getOutterNum: function () {
  //     console.log(this.$store.state.leftNameData);
  //     return this.$store.state.leftNameData
  //   }
  // },
  mounted() {
    this.activePath = sessionStorage.getItem("yjSmart-keyPath") || "/index";
    this.roleModule();
  },
  watch: {
    activePath(newValue) {
      if (newValue != this.$route.path) {
        this.activePath = this.$route.path;
      }
    },
  },

  methods: {
    goHome() {
      this.$router.push({ path: "/index" });
    },
    roleModule() {
      var ModuleList = JSON.parse(sessionStorage.getItem("yjSmart-ModuleList"));
      var fristModule = [];
      if (ModuleList.length != 0) {
        this.leftNameData.map((i) => {
          //筛选大模块
          ModuleList.map((item) => {
            if (item.fEncode == i.name) {
              i.title = item.fFullname;
              fristModule.push(i);
            }
          });
        });

        fristModule.map((item) => {
          //只有两级
          item["childrens"] = [];
          item.children.map((i) => {
            ModuleList.map((j) => {
              if (i.name == j.fEncode) {
                // console.log(i, j);
                i.title = j.fFullname;
                item["childrens"].push(i);
              }
            });
          });
          item.children = item.childrens;
        });
        // 有三级
        fristModule.map((item) => {
          item.children.map((i) => {
            //二级
            i["childrens"] = [];
            i.children.map((h) => {
              //循环3级
              ModuleList.map((j) => {
                if (h.name == j.fEncode) {
                  h.title = j.fFullname;
                  i["childrens"].push(h);
                }
              });
            });

            i.children = i.childrens;
          });
        });

        fristModule.map((item) => {
          item.children.map((i) => {
            if (i.rank == 2) {
              i.path2 = "";
              console.log("BAOCUO");
              ModuleList.map((j) => {
                if (i.name2 == j.fEncode) {
                  i.path2 = i.path + "Text";
                }
              });
            }
          });
        });

        this.leftNameData = fristModule;
      } else if (
        JSON.parse(sessionStorage.getItem("yjSmart-userInfo")).enCode == "System"
      ) {
        this.getTree();
      } else {
        this.leftNameData = [];
        return this.$message.error("请配置对应权限");
      }
    },

    leftNameDataData(val, data) {
      // val:树接口  data:本地
      let list = [];
      if (!Array.isArray(data)) {
        return list;
      }
      list = data.map((item) => {
        if (item.children.length > 0) {
          this.leftNameDataData(val, item.children);
        }
        val.map((i) => {
          // 接口等于本地
          if (i.fEncode == item.name) {
            item.title = i.fFullname;
          }
        });

        return item;
      });

      return list;
    },
    getTree() {
      getBaseModuleList({
        fModuleid: "",
      })
        .then((res) => {
          if (res.errcode == 0) {
            var companyTree = res.data.baseModuleList;
            this.leftNameData = this.leftNameDataData(
              companyTree,
              this.leftNameData
            );
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch(() => {
          return false;
        });
    },
    reload() {
      this.isReloadAlive = false;
      this.$nextTick(function () {
        this.isReloadAlive = true;
      });
    },

    openMenu(keyPath) {
      if (keyPath != "null") {
        sessionStorage.setItem("yjSmart-keyPath", keyPath);
      }
    },
    // 是否全屏
    screen() {
      let element = document.documentElement;
      if (this.fullscreen) {
        if (document.exitFullscreen) {
          document.exitFullscreen();
        } else if (document.webkitCancelFullScreen) {
          document.webkitCancelFullScreen();
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen();
        } else if (document.msExitFullscreen) {
          document.msExitFullscreen();
        }
      } else {
        if (element.requestFullscreen) {
          element.requestFullscreen();
        } else if (element.webkitRequestFullScreen) {
          element.webkitRequestFullScreen();
        } else if (element.mozRequestFullScreen) {
          element.mozRequestFullScreen();
        } else if (element.msRequestFullscreen) {
          // IE11
          element.msRequestFullscreen();
        }
      }
      this.fullscreen = !this.fullscreen;
    },

    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },

    back() {
      this.$confirm("是否继续退出?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          window.sessionStorage.removeItem("yjSmart-token");
          this.$router.push("/");
        })
        .catch(() => {
          this.loading = false;
          this.$message.info("取消退出");
        });
    },
  },
};
</script>
<style scoped lang="less">
a {
  color: #fff;
}

.home {
  width: 100%;
  height: 100%;
}

.home_header {
  background-color: #1559ba;
  text-align: center;
  height: 10%;
  width: 100%;
}

/* 右侧 */
.main_right {
  width: 100%;
  height: 100%;
  padding: 0 15px;
  box-sizing: border-box;
  overflow: hidden;
}

.main_right_position {
  text-align: left;
  width: 100%;
}

.main_right_table {
  width: 100%;
  height: 90%;
}

.content {
  width: 100%;
  height: 100%;
  padding: 0 5px;
  box-sizing: border-box;
}

.toggle-button {
  display: block;
  background-color: #1559ba;
  font-size: 17px;
  line-height: 30px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  background: #0c204d !important;
}

.el-aside {
  position: relative;
}

.userInfo {
  position: absolute;
  width: 100%;
  height: 50px;
  bottom: 0;
  display: flex;
  align-items: center;
  border-top: 1px solid #63656b;
  padding: 0 35px 0 25px;
  box-sizing: border-box;
  background: #0c204d;

  .iconsv {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    line-height: 0px;
    overflow: hidden;
    margin-right: 15px;

    .iconimg {
      width: 100%;
      height: 100%;
    }
  }

  .name {
    height: 100%;
    line-height: 49px;

    font-size: 12px;
    color: #a6acbc;
  }
}

#meun /deep/.el-menu-vertical-demo {
  height: calc(100% - 82px) !important;
  overflow-y: auto;
}

.secondaryView {
  display: flex;
  align-items: center;
}
</style>
